package com.stars.pao.ojsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stars.pao.ojcommoncore.domain.R;
import com.stars.pao.ojcommoncore.enums.ResultCode;
import com.stars.pao.ojsystem.controller.LoginResult;
import com.stars.pao.ojsystem.domain.SysUser;
import com.stars.pao.ojsystem.domain.SysUserSavaDTO;
import com.stars.pao.ojsystem.mapper.SysUserMapper;
import com.stars.pao.ojsystem.service.ISysUserService;
import com.stars.pao.ojsystem.utils.BCryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public R<Void> login(String userAccount, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper =  new LambdaQueryWrapper<>();
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper
                .select(SysUser::getPassword).eq(SysUser::getUserAccount,userAccount));

//        R result = new R();
        // 异常 int a = 100 / 0;
        if(sysUser == null) {
//            result.setCode(ResultCode.FAILED_USER_NOT_EXISTS.getCode());
//            result.setMsg(ResultCode.FAILED_USER_NOT_EXISTS.getMsg());
//            return result;
            return R.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(BCryptUtils.matchesPassword(password,sysUser.getPassword())) {
//            result.setCode(ResultCode.SUCCESS.getCode());
//            result.setMsg(ResultCode.SUCCESS.getMsg());
//            return result;
            return R.ok();
        }
//        result.setCode(ResultCode.FAILED_LOGIN.getCode());
//        result.setMsg(ResultCode.FAILED_LOGIN.getMsg());
//        return result;
        return R.fail(ResultCode.FAILED_LOGIN);
    }
}
